//最小栈
class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minstack;
    

    public MinStack() {
        stack = new Stack<>();
        minstack = new Stack<>();
    }
    
    public void push(int val) {
        //1.push的时候普通栈一定要放元素
        stack.push(val);
        //2.最小栈如果是空的也要放，不为空且要存放的元素小于最小栈的栈顶，那么也要放到最小栈
        if(minstack.empty()){
            minstack.push(val);
        }else{
            if(val <= minstack.peek()){
                minstack.push(val);
            }
        }
    }
    
    public void pop() {
        //1.pop的时候要和普通栈的栈顶元素进行比较
        //2.如果pop的元素和普通栈的元素是一样的那么两个栈都要pop
        //3.如果pop的元素和普通栈的元素是不一样的那么只需要pop普通栈
        int val = stack.pop();
        if(!minstack.empty()){
            if(val == minstack.peek()){
                minstack.pop();
            }
        }
    }
    
    //peek 获取普通栈的栈顶元素值
    public int top() {
        return stack.peek();
    }
    
    //最小栈的peek 每次通过这个方法获取最小值
    public int getMin() {
        if(!minstack.empty()){
           return minstack.peek();
        }
        return -1;
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */